package com.tuniu.agents.advisor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Agent global environment variable storage aspect - in-memory implementation
 */
public class InMemoryChatEnvironmentMemory implements ChatEnvironmentMemory {

    Map<String, Map<String, Object>> chatExtendContent = new ConcurrentHashMap<>();

    public InMemoryChatEnvironmentMemory() {
        // Constructor for InMemoryChatEnvironmentMemory
    }

    @Override
    public void add(String conversationId, Map<String, Object> message) {
        Map<String, Object> all = this.chatExtendContent.get(conversationId);
        if (all == null) {
            this.chatExtendContent.put(conversationId, message);
        } else {
            all.putAll(message);
        }
    }

    @Override
    public Map<String, Object> get(String conversationId) {
        Map<String, Object> all = this.chatExtendContent.get(conversationId);
        return all != null ? all : Map.of();
    }

    @Override
    public Object get(String conversationId, String key) {
        Map<String, Object> all = this.chatExtendContent.get(conversationId);
        Object value = all.get(key);
        return value != null ? value : null;
    }

    @Override
    public Boolean exist(String conversationId) {
        return this.chatExtendContent.get(conversationId) != null;
    }

    @Override
    public void clear(String conversationId, String key) {
        // Clear a specific key for the given conversationId (not implemented)
    }

    @Override
    public void clear(String conversationId) {
        this.chatExtendContent.remove(conversationId);
    }
}

